package demo04;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;

/**
 * DBUtils框架的介绍
 *
 * DBUtils类:主要负责关闭连接,释放资源,开启事务等操作
 *
 * QueryRunner:负责我们对象数据库的CURD操作(核心类)
 *
 * ResultSetHandler:结果集处理类,帮我我们处理结果集(帮助我们封装数据的)
 *
 * QueryRunner类的使用:
 * 		构造:
 * 		public QueryRunner(DataSource ds);//需要一个连接池
 * 		public QueryRunner();//不需要连接,用来进行事务操作,明天说
 * 		方法:
 * 		 	int update(String sql, Object... params);//主要执行增删改
 * 			query(String sql, ResultSetHandler<T> rsh, Object... params);//主要执行查询
 *
 *
 * @author yingpeng
 *
 */
public class DBUtilsDemo {

	public static void main(String[] args) throws SQLException {
		// TODO Auto-generated method stub
		insert();
//		delete();
//		update();
	}

	//增
	public static void insert() throws SQLException{
		QueryRunner qr = new QueryRunner(C3P0Utils02.getDataSource());
		//插入
		Object[] obj = {16,"皮鞋类"};
		int rows = qr.update("insert into category (cid,cname) values (?,?)", obj);
		System.out.println(rows);

//		System.out.println(	new QueryRunner(C3P0Utils02.getDataSource()).
//				update("insert into category (cid,cname) values (?,?)",
//						new Object[]{17,"成人用品类"}));
	}
	//删
	public static void delete() throws SQLException{
		//1.创建QueryRunner对象,给定连接池
		QueryRunner qr = new QueryRunner(C3P0Utils02.getDataSource());
		//2.执行
		int rows = qr.update("delete from category where cid = ?", new Object[]{17});
		System.out.println(rows);
	}
	//修改
	public static void update() throws SQLException{
		//1.创建QueryRunner对象,给定连接池
		QueryRunner qr = new QueryRunner(C3P0Utils02.getDataSource());
		//2.执行
		int rows = qr.update("update category set cname = ? where cid = ?", new Object[]{"运动鞋类",16});
		System.out.println(rows);
	}
	//查询
	public static void query(){

	}

}
